<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python行天下 | 川哥编程路</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/blog/image/favicon.ico">
    <link rel="stylesheet" href="/blog/css/style.css">
    <meta name="description" content="日拱一卒无有尽，功不唐捐终入海">
    
    <link rel="preload" href="/blog/assets/css/0.styles.797dfc43.css" as="style"><link rel="preload" href="/blog/assets/js/app.160d1732.js" as="script"><link rel="preload" href="/blog/assets/js/2.553eb4dc.js" as="script"><link rel="preload" href="/blog/assets/js/26.81508f46.js" as="script"><link rel="preload" href="/blog/assets/js/3.4c6b6694.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.33b61760.js"><link rel="prefetch" href="/blog/assets/js/11.e936ed72.js"><link rel="prefetch" href="/blog/assets/js/12.06a8905b.js"><link rel="prefetch" href="/blog/assets/js/13.43d57349.js"><link rel="prefetch" href="/blog/assets/js/14.9a947fce.js"><link rel="prefetch" href="/blog/assets/js/15.00675827.js"><link rel="prefetch" href="/blog/assets/js/16.4240201a.js"><link rel="prefetch" href="/blog/assets/js/17.4e8e9f2d.js"><link rel="prefetch" href="/blog/assets/js/18.c0804ecf.js"><link rel="prefetch" href="/blog/assets/js/19.e374c696.js"><link rel="prefetch" href="/blog/assets/js/20.dc1f2462.js"><link rel="prefetch" href="/blog/assets/js/21.2550a5e8.js"><link rel="prefetch" href="/blog/assets/js/22.64bef411.js"><link rel="prefetch" href="/blog/assets/js/23.56de7058.js"><link rel="prefetch" href="/blog/assets/js/24.7de4d292.js"><link rel="prefetch" href="/blog/assets/js/25.3483b9cb.js"><link rel="prefetch" href="/blog/assets/js/27.f884547d.js"><link rel="prefetch" href="/blog/assets/js/28.fc8f3edd.js"><link rel="prefetch" href="/blog/assets/js/29.6df73fc7.js"><link rel="prefetch" href="/blog/assets/js/30.92ddc0da.js"><link rel="prefetch" href="/blog/assets/js/31.bc595735.js"><link rel="prefetch" href="/blog/assets/js/32.f6ddf561.js"><link rel="prefetch" href="/blog/assets/js/33.ba353a20.js"><link rel="prefetch" href="/blog/assets/js/34.23530c8e.js"><link rel="prefetch" href="/blog/assets/js/35.98587bae.js"><link rel="prefetch" href="/blog/assets/js/4.e380c258.js"><link rel="prefetch" href="/blog/assets/js/5.9b25da44.js"><link rel="prefetch" href="/blog/assets/js/6.3d19bdbc.js"><link rel="prefetch" href="/blog/assets/js/7.b8795dc7.js"><link rel="prefetch" href="/blog/assets/js/8.9332e1e8.js"><link rel="prefetch" href="/blog/assets/js/9.875f1e10.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.797dfc43.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">川哥编程路</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="php" class="dropdown-title"><span class="title">php</span> <span class="arrow down"></span></button> <button type="button" aria-label="php" class="mobile-dropdown-title"><span class="title">php</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/php/base/" class="nav-link">
  通用篇
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/laravel/" class="nav-link">
  Laravel
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/skill/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/nginx/" class="nav-link">
  nginx
</a></div><div class="nav-item"><a href="/blog/print/" class="nav-link">
  print
</a></div><div class="nav-item"><a href="/blog/mysql/" class="nav-link">
  mysql
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="web" class="dropdown-title"><span class="title">web</span> <span class="arrow down"></span></button> <button type="button" aria-label="web" class="mobile-dropdown-title"><span class="title">web</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/web/layout/" class="nav-link">
  css布局
</a></li><li class="dropdown-item"><!----> <a href="/blog/web/case/" class="nav-link">
  css实例
</a></li></ul></div></div><div class="nav-item"><a href="/blog/js/" class="nav-link">
  js
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="python" class="dropdown-title"><span class="title">python</span> <span class="arrow down"></span></button> <button type="button" aria-label="python" class="mobile-dropdown-title"><span class="title">python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/python/base/" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  python基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/python/machine/" class="nav-link">
  机器学习
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Golang" class="dropdown-title"><span class="title">Golang</span> <span class="arrow down"></span></button> <button type="button" aria-label="Golang" class="mobile-dropdown-title"><span class="title">Golang</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/go/env/" class="nav-link">
  前言和环境
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/grammar/" class="nav-link">
  语法基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/base/" class="nav-link">
  函数和数组(切片)
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/map/" class="nav-link">
  Map,指针,值传递,引用传递
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/struct/" class="nav-link">
  结构体
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/gin/" class="nav-link">
  Gin
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/concurrent/" class="nav-link">
  go并发
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/standard/" class="nav-link">
  常用标准库
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/skills/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/docker/" class="nav-link">
  docker
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="vue" class="dropdown-title"><span class="title">vue</span> <span class="arrow down"></span></button> <button type="button" aria-label="vue" class="mobile-dropdown-title"><span class="title">vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/vue/base/" class="nav-link">
  vue基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/assembly/" class="nav-link">
  组件化编程
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/problem/" class="nav-link">
  problem
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/uniApp/" class="nav-link">
  uniApp
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tool/" class="nav-link">
  tool
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="php" class="dropdown-title"><span class="title">php</span> <span class="arrow down"></span></button> <button type="button" aria-label="php" class="mobile-dropdown-title"><span class="title">php</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/php/base/" class="nav-link">
  通用篇
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/laravel/" class="nav-link">
  Laravel
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/skill/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/nginx/" class="nav-link">
  nginx
</a></div><div class="nav-item"><a href="/blog/print/" class="nav-link">
  print
</a></div><div class="nav-item"><a href="/blog/mysql/" class="nav-link">
  mysql
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="web" class="dropdown-title"><span class="title">web</span> <span class="arrow down"></span></button> <button type="button" aria-label="web" class="mobile-dropdown-title"><span class="title">web</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/web/layout/" class="nav-link">
  css布局
</a></li><li class="dropdown-item"><!----> <a href="/blog/web/case/" class="nav-link">
  css实例
</a></li></ul></div></div><div class="nav-item"><a href="/blog/js/" class="nav-link">
  js
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="python" class="dropdown-title"><span class="title">python</span> <span class="arrow down"></span></button> <button type="button" aria-label="python" class="mobile-dropdown-title"><span class="title">python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/python/base/" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  python基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/python/machine/" class="nav-link">
  机器学习
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Golang" class="dropdown-title"><span class="title">Golang</span> <span class="arrow down"></span></button> <button type="button" aria-label="Golang" class="mobile-dropdown-title"><span class="title">Golang</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/go/env/" class="nav-link">
  前言和环境
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/grammar/" class="nav-link">
  语法基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/base/" class="nav-link">
  函数和数组(切片)
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/map/" class="nav-link">
  Map,指针,值传递,引用传递
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/struct/" class="nav-link">
  结构体
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/gin/" class="nav-link">
  Gin
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/concurrent/" class="nav-link">
  go并发
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/standard/" class="nav-link">
  常用标准库
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/skills/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/docker/" class="nav-link">
  docker
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="vue" class="dropdown-title"><span class="title">vue</span> <span class="arrow down"></span></button> <button type="button" aria-label="vue" class="mobile-dropdown-title"><span class="title">vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/vue/base/" class="nav-link">
  vue基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/assembly/" class="nav-link">
  组件化编程
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/problem/" class="nav-link">
  problem
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/uniApp/" class="nav-link">
  uniApp
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tool/" class="nav-link">
  tool
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span></span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/python/base/" aria-current="page" class="active sidebar-link">Python行天下</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#pip包管理工具" class="sidebar-link">pip包管理工具</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#pip最常用命令" class="sidebar-link">pip最常用命令</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#pip源选择" class="sidebar-link">pip源选择</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#python多版本情况" class="sidebar-link">python多版本情况</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/python/base/#虚拟环境和git管理" class="sidebar-link">虚拟环境和Git管理</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#常用语法" class="sidebar-link">常用语法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#zip压缩文件" class="sidebar-link">zip压缩文件</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#递归创建文件夹" class="sidebar-link">递归创建文件夹</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#断点打印" class="sidebar-link">断点打印</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#打印函数" class="sidebar-link">打印函数</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#获取长度" class="sidebar-link">获取长度</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#获取对象类型" class="sidebar-link">获取对象类型</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#对象和字符串互转" class="sidebar-link">对象和字符串互转</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#条件控制" class="sidebar-link">条件控制</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#while循环" class="sidebar-link">while循环</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#for循环" class="sidebar-link">for循环</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#break和continue" class="sidebar-link">break和continue</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#pass语句" class="sidebar-link">pass语句</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/python/base/#时间和日期" class="sidebar-link">时间和日期</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#获取当前格式化时间" class="sidebar-link">获取当前格式化时间</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#指定时间戳格式化" class="sidebar-link">指定时间戳格式化</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#指定格式化时间转时间戳" class="sidebar-link">指定格式化时间转时间戳</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#获取当前时间戳" class="sidebar-link">获取当前时间戳</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/python/base/#字符串" class="sidebar-link">字符串</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#字符串拼接" class="sidebar-link">字符串拼接</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#字符串截取" class="sidebar-link">字符串截取</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#字符串第一次出现的位置" class="sidebar-link">字符串第一次出现的位置</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#字符串最后一次出现的位置" class="sidebar-link">字符串最后一次出现的位置</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/python/base/#django安装和运行" class="sidebar-link">Django安装和运行</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#安装diango" class="sidebar-link">安装Diango</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#查看安装的django版本" class="sidebar-link">查看安装的Django版本</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#创建django项目" class="sidebar-link">创建Django项目</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#项目目录" class="sidebar-link">项目目录</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#运行django-web服务" class="sidebar-link">运行Django Web服务</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#ip访问报错" class="sidebar-link">ip访问报错</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#git上拉django项目快速运行" class="sidebar-link">git上拉Django项目快速运行</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/python/base/#django践行" class="sidebar-link">Django践行</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/python/base/#创建项目app-模块" class="sidebar-link">创建项目app(模块)</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#返回页面内容给浏览器" class="sidebar-link">返回页面内容给浏览器</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#url路由" class="sidebar-link">url路由</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#路由子表" class="sidebar-link">路由子表</a></li><li class="sidebar-sub-header"><a href="/blog/python/base/#django链接mysql" class="sidebar-link">Django链接MYSQL</a></li></ul></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="python行天下"><a href="#python行天下" class="header-anchor">#</a> Python行天下</h1> <h2 id="pip包管理工具"><a href="#pip包管理工具" class="header-anchor">#</a> pip包管理工具</h2> <p>pip 是 Python 包管理工具，该工具提供了对Python 包的查找、下载、安装、卸载的功能。目前只要你正常安装python之后, 则是已经自带了该工具。</p> <h3 id="pip最常用命令"><a href="#pip最常用命令" class="header-anchor">#</a> pip最常用命令</h3> <ul><li><p>显示pip版本和路径</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip <span class="token operator">-</span><span class="token operator">-</span>version
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>升级pip</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># windows</span>
python <span class="token operator">-</span>m pip install <span class="token operator">-</span><span class="token operator">-</span>upgrade pip
<span class="token comment"># linux</span>
pip install <span class="token operator">-</span><span class="token operator">-</span>upgrade pip
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>pip安装某个包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install SomePackage              <span class="token comment"># 最新版本</span>
pip install SomePackage<span class="token operator">==</span><span class="token number">1.0</span><span class="token number">.4</span>       <span class="token comment"># 指定版本</span>
pip install <span class="token string">'SomePackage&gt;=1.0.4'</span>     <span class="token comment"># 最小版本</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><font style="color:blue;">eg:</font>  安装Django , 只需要执行</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install Django<span class="token operator">==</span><span class="token number">1.7</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>升级包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install <span class="token operator">-</span><span class="token operator">-</span>upgrade SomePackage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;">注意:</font>升级指定的包，通过使用==, &gt;=, &lt;=, &gt;, &lt; 来指定一个版本号。</p></li> <li><p>卸载包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip uninstall SomePackage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>搜索包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip search SomePackage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>显示安装包信息</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip show 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>查看指定包的详细信息</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip show <span class="token operator">-</span>f SomePackage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>列出已安装的包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip <span class="token builtin">list</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>查看可升级的包</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip <span class="token builtin">list</span> <span class="token operator">-</span>o
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h3 id="pip源选择"><a href="#pip源选择" class="header-anchor">#</a> pip源选择</h3> <p>很多时候，比如网络不给力，连接超时、防火墙阻挡等等各种原因，我们可能无法从Python官方的PyPi仓库进行pip安装，这时候可以选择国内的第三方源，推荐使用豆瓣源，速度不错。</p> <ul><li><p>查看当前源</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip config <span class="token builtin">list</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>命令行修改源</p> <p><font style="color:blue;">临时使用某个源安装包:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install <span class="token operator">-</span>i https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>tuna<span class="token punctuation">.</span>tsinghua<span class="token punctuation">.</span>edu<span class="token punctuation">.</span>cn<span class="token operator">/</span>simple Django
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;">永久配置源地址为某个源:</font>   比如修改源地址为豆瓣源</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip config <span class="token builtin">set</span> <span class="token keyword">global</span><span class="token punctuation">.</span>index<span class="token operator">-</span>url https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>douban<span class="token punctuation">.</span>com<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>常用源</p> <ul><li><p>默认源</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>Python<span class="token punctuation">.</span>org<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>阿里云</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>http<span class="token punctuation">:</span><span class="token operator">//</span>mirrors<span class="token punctuation">.</span>aliyun<span class="token punctuation">.</span>com<span class="token operator">/</span>pypi<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>清华大学</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>tuna<span class="token punctuation">.</span>tsinghua<span class="token punctuation">.</span>edu<span class="token punctuation">.</span>cn<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>中国科技大学</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>mirrors<span class="token punctuation">.</span>ustc<span class="token punctuation">.</span>edu<span class="token punctuation">.</span>cn<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>豆瓣</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>https<span class="token punctuation">:</span><span class="token operator">//</span>pypi<span class="token punctuation">.</span>douban<span class="token punctuation">.</span>com<span class="token operator">/</span>simple<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul></li></ul> <h3 id="python多版本情况"><a href="#python多版本情况" class="header-anchor">#</a> python多版本情况</h3> <p>如果 Python2 和 Python3 同时有 pip，则使用方法如下：</p> <ul><li><p>Python2</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>python2 <span class="token operator">-</span>m pip install XXX
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>python3</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>python3 <span class="token operator">-</span>m pip install XXX
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h2 id="虚拟环境和git管理"><a href="#虚拟环境和git管理" class="header-anchor">#</a> 虚拟环境和Git管理</h2> <ul><li><p>就是用来为每一个项目创建一套“独立隔离”的Python运行环境的工具;</p></li> <li><p>利用<code>PyCharm</code>工具直接创建项目的时候生成虚拟环境; 避免手工配置产生的问题!</p></li> <li><p>venv配合pip快速建立开发环境</p> <ul><li><p>venv虚拟环境配合pip的freeze功能，可以一次性快捷的将整个环境下的第三方模块全部记录下来：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip freeze <span class="token operator">&gt;</span> requirements<span class="token punctuation">.</span>txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>对应的，也可以用它一次性通过在别的机器上或虚拟环境里，将文件里列出的第三方库安装起来。只需要使用命令:</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install <span class="token operator">-</span>r requirements<span class="token punctuation">.</span>txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul></li> <li><p>git管理: 提交模块库列表记录和代码记录, 然后批量下载安装模块</p> <ul><li><p><code>.gitignore</code></p> <div class="language-gitignore line-numbers-mode"><pre class="language-gitignore"><code><span class="token entry string">.idea<span class="token punctuation">/</span></span>
<span class="token entry string"><span class="token operator">*</span><span class="token punctuation">/</span>__pycache__<span class="token punctuation">/</span><span class="token operator">*</span></span>
<span class="token entry string">__pycache__<span class="token punctuation">/</span></span>
<span class="token entry string">venv<span class="token punctuation">/</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>每次git前, 记录下第三方模块</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip freeze <span class="token operator">&gt;</span> requirements<span class="token punctuation">.</span>txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>安装记录下来的所有第三方模块</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install <span class="token operator">-</span>r requirements<span class="token punctuation">.</span>txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul></li></ul> <h2 id="常用语法"><a href="#常用语法" class="header-anchor">#</a> 常用语法</h2> <h3 id="zip压缩文件"><a href="#zip压缩文件" class="header-anchor">#</a> zip压缩文件</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token keyword">import</span> zipfile


<span class="token keyword">def</span> <span class="token function">zip_dir</span><span class="token punctuation">(</span>source_dir<span class="token punctuation">,</span> output_filename<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
     压缩指定文件夹
     :param source_dir: 目标文件夹路径 eg: E:\\test
     :param output_filename:  压缩文件保存路径+XXXX.zip# 压缩包存放目录 eg:  order_100
     :return:
     &quot;&quot;&quot;</span>
    source_store <span class="token operator">=</span> <span class="token string">'./source_store/orderZip/'</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>source_store<span class="token punctuation">)</span><span class="token punctuation">:</span>
        os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>source_store<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token number">0o755</span><span class="token punctuation">)</span>
    source_zip <span class="token operator">=</span> zipfile<span class="token punctuation">.</span>ZipFile<span class="token punctuation">(</span>source_store <span class="token operator">+</span> output_filename <span class="token operator">+</span> <span class="token string">'.zip'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">,</span> zipfile<span class="token punctuation">.</span>ZIP_DEFLATED<span class="token punctuation">)</span>
    <span class="token keyword">for</span> root<span class="token punctuation">,</span> dirs<span class="token punctuation">,</span> files <span class="token keyword">in</span> os<span class="token punctuation">.</span>walk<span class="token punctuation">(</span>source_dir<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> <span class="token builtin">file</span> <span class="token keyword">in</span> files<span class="token punctuation">:</span>
            <span class="token comment"># 第二个参数名表示的是文件的名字，这样做就可以保证压缩以后不会出现多层文件目录的情况。</span>
            source_zip<span class="token punctuation">.</span>write<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'./%s/%s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>output_filename<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    source_zip<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="递归创建文件夹"><a href="#递归创建文件夹" class="header-anchor">#</a> 递归创建文件夹</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>source_store <span class="token operator">=</span> <span class="token string">'./source_store/order_%s/'</span><span class="token operator">%</span><span class="token number">100</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>source_store<span class="token punctuation">)</span><span class="token punctuation">:</span>
    os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>source_store<span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token number">0o755</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="断点打印"><a href="#断点打印" class="header-anchor">#</a> 断点打印</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> sys    
sys<span class="token punctuation">.</span>exit<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 断点, 后面不执行</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="打印函数"><a href="#打印函数" class="header-anchor">#</a> 打印函数</h3> <p>打印函数, 把括号内的内容显示</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token number">123123</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="获取长度"><a href="#获取长度" class="header-anchor">#</a> 获取长度</h3> <p>Python <code>len()</code> 方法返回对象（字符、列表、元组、字典等）长度或项目个数。</p> <p><font style="color:blue;">语法:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">len</span><span class="token punctuation">(</span> s <span class="token punctuation">)</span>
<span class="token operator">+</span>	参数<span class="token punctuation">:</span> s <span class="token operator">-</span><span class="token operator">-</span> 对象
<span class="token operator">+</span>	返回<span class="token punctuation">:</span> 对象长度
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><font style="color:blue;">demo:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">'a国6美6人'</span>
<span class="token builtin">list</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
<span class="token builtin">dict</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> <span class="token string">'runoob'</span><span class="token punctuation">,</span> <span class="token string">'likes'</span><span class="token punctuation">:</span> <span class="token number">123</span><span class="token punctuation">,</span> <span class="token string">'url'</span><span class="token punctuation">:</span> <span class="token string">'www.runoob.com'</span><span class="token punctuation">}</span>
tup2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
<span class="token comment"># 获取长度</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 6</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span><span class="token builtin">list</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 4</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span><span class="token builtin">dict</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 3</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>tup2<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 5</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="获取对象类型"><a href="#获取对象类型" class="header-anchor">#</a> 获取对象类型</h3> <p><code>type()</code>函数如果你只有第一个参数则返回对象的类型</p> <p><font style="color:blue;">语法:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">type</span><span class="token punctuation">(</span><span class="token builtin">object</span><span class="token punctuation">)</span>

<span class="token operator">+</span> 返回对象类型
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code>demoStr <span class="token operator">=</span> <span class="token string">'a国6美6人'</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class 'str'&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class 'list'&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class 'tuple'&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'a'</span><span class="token punctuation">:</span> <span class="token number">123</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class 'dict'&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="对象和字符串互转"><a href="#对象和字符串互转" class="header-anchor">#</a> 对象和字符串互转</h3> <p><strong>json</strong> 模块提供了一种很简单的方式来编码和解码JSON数据, 列表, 字典, 元组等python数据结构编码成json字符串用: <font style="color:blue;"> json.dumps()</font>, json字符串解码还原为python数据结构: <font style="color:blue;">json.loads() </font> 。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> json

data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> <span class="token string">'ACME'</span><span class="token punctuation">,</span> <span class="token string">'shares'</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'price'</span><span class="token punctuation">:</span> <span class="token number">542.23</span><span class="token punctuation">}</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># &lt;class 'dict'&gt;</span>
<span class="token comment"># 将python对象data转换为json字符串</span>
json_str <span class="token operator">=</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>json_str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># &lt;class 'str'&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>json_str<span class="token punctuation">)</span> <span class="token comment"># {&quot;name&quot;: &quot;ACME&quot;, &quot;shares&quot;: 100, &quot;price&quot;: 542.23}</span>
origin_data <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>json_str<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>origin_data<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># &lt;class 'dict'&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>origin_data<span class="token punctuation">)</span> <span class="token comment"># {'name': 'ACME', 'shares': 100, 'price': 542.23}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><font style="color:blue;">如果你要处理的是文件而不是字符串，你可以使用 json.dump() 和 json.load() 来编码和解码JSON数据</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> json

data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> <span class="token string">'ACME'</span><span class="token punctuation">,</span> <span class="token string">'shares'</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'price'</span><span class="token punctuation">:</span> <span class="token number">542.23</span><span class="token punctuation">}</span>
<span class="token comment"># Writing JSON data</span>
<span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'data.json'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
    <span class="token comment"># data字典转成字符串写入文件data.json</span>
    json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>data<span class="token punctuation">,</span> f<span class="token punctuation">)</span>

<span class="token comment"># Reading data back</span>
<span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'data.json'</span><span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
    data <span class="token operator">=</span> json<span class="token punctuation">.</span>load<span class="token punctuation">(</span>f<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class 'dict'&gt;</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>  <span class="token comment"># {'name': 'ACME', 'shares': 100, 'price': 542.23}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="条件控制"><a href="#条件控制" class="header-anchor">#</a> 条件控制</h3> <p>Python 条件语句是通过一条或多条语句的执行结果（True 或者 False）来决定执行的代码块。</p> <p><font style="color:blue;">if语句的关键字为：<strong>if – elif – else</strong></font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>a <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">if</span> a <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'a等于3'</span><span class="token punctuation">)</span>
<span class="token keyword">elif</span> a <span class="token operator">&gt;</span> <span class="token number">3</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'a大于3'</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'a小于3'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="while循环"><a href="#while循环" class="header-anchor">#</a> while循环</h3> <p>Python 中的循环语句有 for 和 while</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>n <span class="token operator">=</span> <span class="token number">100</span>
<span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token number">0</span>
counter <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> counter <span class="token operator">&lt;=</span> n<span class="token punctuation">:</span>
    <span class="token builtin">sum</span> <span class="token operator">+=</span> counter
    counter <span class="token operator">+=</span> <span class="token number">1</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;1 到 %d 之和为: %d&quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>n<span class="token punctuation">,</span><span class="token builtin">sum</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 1 到 100 之和为: 5050</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><font style="color:blue;">如果 while 后面的条件语句为 false 时，则执行 else 的语句块:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>count <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">while</span> count <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">:</span>
   <span class="token keyword">print</span> <span class="token punctuation">(</span>count<span class="token punctuation">,</span> <span class="token string">&quot; 小于 5&quot;</span><span class="token punctuation">)</span>
   count <span class="token operator">=</span> count <span class="token operator">+</span> <span class="token number">1</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
   <span class="token keyword">print</span> <span class="token punctuation">(</span>count<span class="token punctuation">,</span> <span class="token string">&quot; 大于或等于 5&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="for循环"><a href="#for循环" class="header-anchor">#</a> for循环</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> data<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token comment"># 正常结束for循环, 执行, for循环中出现了break跳出了循环, else不执行</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'最后一次'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="break和continue"><a href="#break和continue" class="header-anchor">#</a> break和continue</h3> <ul><li><strong>break</strong> 可以跳出 for 和 while 的循环体; 如果你从 for 或 while 循环中终止，任何对应的循环 else 块将不执</li> <li><strong>continue</strong> 语句被用来告诉 Python 跳过当前循环块中的剩余语句，然后继续进行下一轮循环</li></ul> <h3 id="pass语句"><a href="#pass语句" class="header-anchor">#</a> pass语句</h3> <p>Python pass是空语句，是为了保持程序结构的完整性。</p> <p>pass 不做任何事情，一般用做占位语句，如下实例</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> data<span class="token punctuation">:</span>
    <span class="token keyword">pass</span> <span class="token comment"># 空语句, 保持程序结构的完整性。pass 不做任何事情，一般用做占位语句</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'最后一次'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="时间和日期"><a href="#时间和日期" class="header-anchor">#</a> 时间和日期</h2> <h3 id="获取当前格式化时间"><a href="#获取当前格式化时间" class="header-anchor">#</a> 获取当前格式化时间</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> datetime
<span class="token keyword">import</span> time

<span class="token comment"># 获取当前格式化时间</span>
now_time <span class="token operator">=</span> datetime<span class="token punctuation">.</span>datetime<span class="token punctuation">.</span>now<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 2021-11-25 10:47:31.140344</span>
<span class="token comment"># 转换为指定的格式</span>
otherStyleTime <span class="token operator">=</span> now_time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前时间格式化为: '</span><span class="token punctuation">,</span> otherStyleTime<span class="token punctuation">)</span>  <span class="token comment"># 当前时间格式化为:  2021-11-25 10:47:31</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="指定时间戳格式化"><a href="#指定时间戳格式化" class="header-anchor">#</a> 指定时间戳格式化</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> datetime
<span class="token keyword">import</span> time
<span class="token comment"># 指令时间戳格式化</span>
timeStamp <span class="token operator">=</span> <span class="token number">1557502800</span>
<span class="token comment"># 格式化本地时间</span>
<span class="token comment"># time.struct_time(tm_year=2019, tm_mon=5, tm_mday=10, tm_hour=23,</span>
<span class="token comment"># ........tm_min=40, tm_sec=0, tm_wday=4, tm_yday=130, tm_isdst=0)</span>
timeArray <span class="token operator">=</span> time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span>timeStamp<span class="token punctuation">)</span>
otherStyleTime <span class="token operator">=</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span class="token punctuation">,</span> timeArray<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'指定时间格式化为:'</span><span class="token punctuation">,</span> otherStyleTime<span class="token punctuation">)</span>  <span class="token comment"># 指定时间格式化为: 2019-05-10 23:40:00</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="指定格式化时间转时间戳"><a href="#指定格式化时间转时间戳" class="header-anchor">#</a> 指定格式化时间转时间戳</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> time

new_time <span class="token operator">=</span> <span class="token string">'2019-04-27 07:01:46'</span>
<span class="token comment"># 把格式化好的时间转换成元祖</span>
time_tuple  <span class="token operator">=</span> time<span class="token punctuation">.</span>strptime<span class="token punctuation">(</span>new_time<span class="token punctuation">,</span> <span class="token string">'%Y-%m-%d %H:%M:%S'</span><span class="token punctuation">)</span>
<span class="token comment"># 把时间元祖转换成时间戳</span>
result <span class="token operator">=</span> time<span class="token punctuation">.</span>mktime<span class="token punctuation">(</span>time_tuple<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="获取当前时间戳"><a href="#获取当前时间戳" class="header-anchor">#</a> 获取当前时间戳</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 获取当前时间戳</span>
now <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 返回float数据 1637808088.3227932</span>
<span class="token comment">#  获取当前时间戳---秒级</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前秒级时间戳为: '</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前秒级时间戳为:  1637808451</span>
<span class="token comment">#  获取当前时间戳---毫秒级</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'当前秒级时间戳为: '</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>now <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 当前秒级时间戳为:  1637808451140</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h2> <h3 id="字符串拼接"><a href="#字符串拼接" class="header-anchor">#</a> 字符串拼接</h3> <ul><li><p>%s的方式, 字符串中使用%s占位，在字符串后使用%替换值来替换</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">'开心的%s唱%s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span><span class="token string">'小明'</span><span class="token punctuation">,</span> <span class="token string">'快乐的小鸟'</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">)</span>  <span class="token comment"># 开心的小明唱快乐的小鸟</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>format()方式, 字符串中使用{}占位，在字符串后使用format替换值来替换</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">'开心的{}唱{}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'小明'</span><span class="token punctuation">,</span> <span class="token number">1234567</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">)</span>  <span class="token comment"># 开心的小明唱1234567</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul> <h3 id="字符串截取"><a href="#字符串截取" class="header-anchor">#</a> 字符串截取</h3> <p>单字符在 Python 中也是作为一个字符串使;</p> <p>Python 访问子字符串，可以使用方括号 <strong>[]</strong> 来截取字符串，字符串的截取的语法格式如下:</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>变量<span class="token punctuation">[</span>头下标<span class="token punctuation">:</span>尾下标<span class="token punctuation">]</span> 
<span class="token comment"># 索引值以 0 为开始值，-1 为从末尾的开始位置; 截取包含头下标元素, 不包含尾下标元素</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><font style="color:blue;">demo:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>demoStr <span class="token operator">=</span> <span class="token string">'a国6美6人'</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># a国6</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 美6人</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 美6</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="字符串第一次出现的位置"><a href="#字符串第一次出现的位置" class="header-anchor">#</a> 字符串第一次出现的位置</h3> <p><code>find()</code>方法检测字符串中是否包含子字符串 str ，如果指定 beg（开始） 和 end（结束） 范围，则检查是否包含在指定范围内，如果指定范围内如果包含指定索引值，返回的是索引值在字符串中的起始位置。如果不包含索引值，返回-1。 <font style="color:blue;">返回<code>-1</code>则表示没有查找到这个字符串</font></p> <p><font style="color:blue;">语法:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">str</span><span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">,</span> beg<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token builtin">len</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span><span class="token punctuation">)</span>

参数<span class="token punctuation">:</span>
    <span class="token number">1.</span> <span class="token builtin">str</span> <span class="token operator">-</span><span class="token operator">-</span> 指定检索的字符串
    <span class="token number">2.</span> beg <span class="token operator">-</span><span class="token operator">-</span> 开始索引，默认为<span class="token number">0</span>
    <span class="token number">3.</span> end <span class="token operator">-</span><span class="token operator">-</span> 结束索引，默认为字符串的长度
返回值<span class="token punctuation">:</span>
    如果包含子字符串返回开始的索引值，否则返回<span class="token operator">-</span><span class="token number">1</span>。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><font style="color:blue;">demo:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>demoStr <span class="token operator">=</span> <span class="token string">'a国6美6人'</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'美6'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 3</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>demoStr<span class="token punctuation">[</span>demoStr<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'美6'</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 美6人</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="字符串最后一次出现的位置"><a href="#字符串最后一次出现的位置" class="header-anchor">#</a> 字符串最后一次出现的位置</h3> <p><code>rfind()</code> 返回字符串最后一次出现的位置，如果没有匹配项则返回-1。 <font style="color:blue;">返回<code>-1</code>则表示没有查找到这个字符串</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token builtin">str</span><span class="token punctuation">.</span>rfind<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">,</span> beg<span class="token operator">=</span><span class="token number">0</span> end<span class="token operator">=</span><span class="token builtin">len</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span><span class="token punctuation">)</span>

参数<span class="token punctuation">:</span>
    <span class="token number">1.</span> <span class="token builtin">str</span> <span class="token operator">-</span><span class="token operator">-</span> 指定检索的字符串
    <span class="token number">2.</span> beg <span class="token operator">-</span><span class="token operator">-</span> 开始索引，默认为<span class="token number">0</span>
    <span class="token number">3.</span> end <span class="token operator">-</span><span class="token operator">-</span> 结束索引，默认为字符串的长度
返回值<span class="token punctuation">:</span>
    如果包含子字符串返回开始的索引值，否则返回<span class="token operator">-</span><span class="token number">1</span>。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><font style="color:blue;">用法同find; 不同之处: find()从左往右查, rfind()是从右往左查的。</font></p> <h2 id="django安装和运行"><a href="#django安装和运行" class="header-anchor">#</a> Django安装和运行</h2> <h3 id="安装diango"><a href="#安装diango" class="header-anchor">#</a> 安装Diango</h3> <p><a href="http://www.byhy.net/tut/webdev/django/02/" target="_blank" rel="noopener noreferrer">安装参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>Django 框架是用Python语言开发的, 所以可以直接pip安装</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install django
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="查看安装的django版本"><a href="#查看安装的django版本" class="header-anchor">#</a> 查看安装的Django版本</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>python <span class="token operator">-</span>m django <span class="token operator">-</span><span class="token operator">-</span>version
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;">demo:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>PS D<span class="token punctuation">:</span>\wwwroot\python<span class="token operator">&gt;</span> python <span class="token operator">-</span>m django <span class="token operator">-</span><span class="token operator">-</span>version
<span class="token number">3.2</span><span class="token number">.9</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="创建django项目"><a href="#创建django项目" class="header-anchor">#</a> 创建Django项目</h3> <ul><li><p><font style="color:blue;">创建一个空的文件夹作为项目目录, 这里新建目录<code>D:\wwwroot\python\studyDjango</code></font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>mkdir studyDjango <span class="token operator">&amp;</span><span class="token operator">&amp;</span> cd studyDjango
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>进入项目目录, 执行命名, 创建<code>manage.py</code> 和 <code>项目配置目录</code> 名为 <code>config</code></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>django<span class="token operator">-</span>admin startproject config <span class="token punctuation">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># window下创建</span>
PS D<span class="token punctuation">:</span>\wwwroot\python<span class="token operator">&gt;</span> cd <span class="token punctuation">.</span>\studyDjango\
PS D<span class="token punctuation">:</span>\wwwroot\python\studyDjango<span class="token operator">&gt;</span> django<span class="token operator">-</span>admin startproject config <span class="token punctuation">.</span>
PS D<span class="token punctuation">:</span>\wwwroot\python\studyDjango<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li></ul> <h3 id="项目目录"><a href="#项目目录" class="header-anchor">#</a> 项目目录</h3> <ul><li><code>studyDjango</code> 项目目录,  项目文件都放在里面</li> <li><code>manage.py</code> 是一个工具脚本，用作项目管理的。以后我们会使用它执行管理操作。</li> <li>里面的 <code>config/</code> 目录是python包。 里面包含项目的重要配置文件。这个目录名字不能随便改，因为manage.py 要用到它。</li> <li><code>config/settings.py</code> 是 Django 项目的配置文件. 包含了非常重要的配置项，以后我们可能需要修改里面的配置。</li> <li><code>config/urls.py</code> 里面存放了 一张表， 声明了前端发过来的各种http请求，分别由哪些函数处理.。</li> <li><code>config/wsgi.py</code> 提供给<code>wsgi web server</code>调用 的接口文件，里面的变量application对应对象实现了 wsgi入口，供<code>wsgi web server</code>调用</li></ul> <h3 id="运行django-web服务"><a href="#运行django-web服务" class="header-anchor">#</a> 运行Django Web服务</h3> <p><font style="color:blue;">项目根目录执行:</font></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>python manage<span class="token punctuation">.</span>py runserver <span class="token number">0.0</span><span class="token number">.0</span><span class="token number">.0</span><span class="token punctuation">:</span><span class="token number">80</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li><p><code>0.0.0.0:80</code> 是指定 web服务绑定的 IP 地址和端口</p></li> <li><p><code>0.0.0.0</code> 表示绑定本机所有的IP地址， 就是可以通过任何一个本机的IP (包括 环回地址 <code>127.0.0.1</code>) 都可以访问我们的服务</p></li> <li><p><code>80</code> 表示是服务启动在80端口上</p></li> <li><p>此时, 请打开浏览器，地址栏输入<code>127.0.0.1</code> 就可以正常访问了!</p></li></ul> <h3 id="ip访问报错"><a href="#ip访问报错" class="header-anchor">#</a> ip访问报错</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>DisallowedHost at <span class="token operator">/</span>
Invalid HTTP_HOST header<span class="token punctuation">:</span> <span class="token string">'192.168.0.110'</span><span class="token punctuation">.</span> You may need to add <span class="token string">'192.168.0.110'</span> to ALLOWED_HOSTS<span class="token punctuation">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><font style="color:blue;">处理:</font></p> <p><code>config/settings.py</code>中添加允许访问的域名, 默认是<code>127.0.0.1</code> 不用添加, 如果添加了其他的ip, <code>127.0.0.1</code>也需要添加</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>ALLOWED_HOSTS <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'127.0.0.1'</span><span class="token punctuation">,</span> <span class="token string">'192.168.0.110'</span><span class="token punctuation">,</span><span class="token string">'localhost'</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="git上拉django项目快速运行"><a href="#git上拉django项目快速运行" class="header-anchor">#</a> git上拉Django项目快速运行</h3> <ol><li>项目拉到本地文件夹</li> <li>pycharm 新建纯python项目, 位置选择为项目拉到本地的文件夹</li> <li>执行<code>pip install -r requirements.txt</code> 安装所有依赖</li> <li>运行<code>python manage.py runserver 0.0.0.0:80</code></li></ol> <h2 id="django践行"><a href="#django践行" class="header-anchor">#</a> Django践行</h2> <p><a href="https://www.liujiangblog.com/" target="_blank" rel="noopener noreferrer">Django学习参考博客<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="创建项目app-模块"><a href="#创建项目app-模块" class="header-anchor">#</a> 创建项目app(模块)</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>python manage<span class="token punctuation">.</span>py startapp api
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>api是项目app名称</li> <li>Django 中的一个app 就是项目里面的一个应用的意思; 类似于php的一个模块, 比如管理后台模块, 里面包含了常用的文件</li> <li>app本质是一个Python包, 里面包含了应用相关的代码文件</li></ul> <h3 id="返回页面内容给浏览器"><a href="#返回页面内容给浏览器" class="header-anchor">#</a> 返回页面内容给浏览器</h3> <p>我们刚才创建的api应用里面有个<code>views.py</code>文件, 这个文件里面通常是写处理http请求的代码的;</p> <p>比如, 我们 设计 凡是浏览器访问的http请求的url地址是<code>/tests/</code>, 就由<code>view.py</code>里面的函数<code>tests</code>来处理, 返回一段字符串给浏览器。</p> <p>打开<code>api/view.py</code>文件, 在里面加入如下内容</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> django<span class="token punctuation">.</span>http <span class="token keyword">import</span> HttpResponse


<span class="token keyword">def</span> <span class="token function">tests</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> HttpResponse<span class="token punctuation">(</span><span class="token string">&quot;我是一条http测试记录&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><font style="color:blue;">注意:</font></p> <ul><li><p>这里面最终的返回结果是 <code>HttpResponse</code> 对象的参数字符串 ，也就是这句话 <code>我是一条http测试记录</code></p></li> <li><p><code>tests</code>的参数 <code>request</code> 是Django中的 <code>HttpRequest</code> 对象，包含了HTTP请求中的信息。</p> <p>后端程序处理请求，常常要根据<strong>请求中的数据</strong>进行相应的处理：</p> <p>比如请求添加一个用户，那么HTTP请求消息中就会携带要添加用户的信息（姓名、登录账号等）。</p> <p>我们写后端的代码，这些信息就在 <code>HttpRequest</code>对象中获取。</p> <p>所以这个参数非常重要</p></li></ul> <p><code>HttpRequest</code> 对象的属性和用法，具体可以<a href="https://docs.djangoproject.com/zh-hans/3.1/ref/request-response/" target="_blank" rel="noopener noreferrer">参考官方文档这里<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>光是定义了这样一个函数不行的，我们需要 <strong>告诉 Django</strong> ：</p> <p>当前端发送过来的HTTP请求 url地址 是 <code>/tests/</code> , 就由 views.py 里面的函数 <code>tests</code> 来处理</p> <p>怎么告诉Django呢？</p> <p>这就是 Django中的 url路由设置。</p> <h3 id="url路由"><a href="#url路由" class="header-anchor">#</a> url路由</h3> <p><code>config/urls.py</code>文件, 是url路由设置的入口文件, 打开这个文件, 在<code>urlpatterns</code>列表变量中添加一个路由信息, 结果如下:</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> django<span class="token punctuation">.</span>urls <span class="token keyword">import</span> path
<span class="token comment"># 别忘了导入 tests 函数</span>
<span class="token keyword">import</span> api<span class="token punctuation">.</span>views
<span class="token keyword">from</span> api<span class="token punctuation">.</span>views <span class="token keyword">import</span> tests

urlpatterns <span class="token operator">=</span> <span class="token punctuation">[</span>
    path<span class="token punctuation">(</span><span class="token string">'admin/'</span><span class="token punctuation">,</span> admin<span class="token punctuation">.</span>site<span class="token punctuation">.</span>urls<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment"># 添加如下的路由记录</span>
    path<span class="token punctuation">(</span><span class="token string">'tests/'</span><span class="token punctuation">,</span> tests<span class="token punctuation">)</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><code>urlpatterns</code> 列表 就是 Django 的 url 路由的入口。</p> <p>里面是一条条的路由记录，我们添加的</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>path<span class="token punctuation">(</span><span class="token string">'tests/'</span><span class="token punctuation">,</span> tests<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>就是告诉 当前端过来的请求 url地址 是 <code>/tests/</code> , 就由 views.py 里面的函数 <code>tests</code> 来处理。</p> <p><code>路由</code> 就是指 ： 根据 HTTP请求的url路径， 设置 由哪个 函数来处理这个请求。</p> <p>通常我们项目代码的修改， Django的测试服务可以自动检测到，并且重新加载，不需要我们重启 Django Web 服务。</p> <p>如果大家想重新启动 Django web 服务， 大家可以在启动web服务的命令行窗口，按ctrl + break（也就是Pause按钮）先停止服务。 然后再次运行启动命令。</p> <p>我们这时, 浏览器输入http://127.0.0.1:1234/tests/, 回车后;  浏览器的请求经过 Django路由后, 选择执行我们定义的函数 <code>tests</code>, 该函数 返回的字符串， 被作为http响应的消息体中的内容返回给 浏览器了。</p> <p>所以浏览器最终显示的就是 我们 <code>tests</code> 函数返回的字符串。</p> <p><font style="color:blue;">注意:</font></p> <ol><li>只要修改了路由表配置，添加了我们自己的路由记录，再去浏览器访问 首页，这里就是 <code>http://127.0.0.1</code> ，前面曾经出现的小火箭欢迎页就不见了！ 会出现一个 404 Not Found 的报错页面。</li> <li>这是正常的，小火箭欢迎页面 是Django在调试模式下，发现路由记录没有添加的时候，缺省作为首页的。 真正的产品是不会使用这个首页的。一旦路由记录发生变动， 就会消失。</li></ol> <h3 id="路由子表"><a href="#路由子表" class="header-anchor">#</a> 路由子表</h3> <p>url 路由表就是可以像上面这样，一个请求对应一个处理函数。</p> <p>但是有的时候，我们的项目比较大的时候， 请求的url 会特别多。这时，我们通常可以将不同的路由记录 按照功能 分拆到不同的 <strong>url路由子表</strong> 文件中。</p> <p>比如，这里我们可以把 访问 的 url 凡是 以 <code>api</code> 开头的全部都 由 <code>api</code>目录下面的 子路由文件 <code>urls.py</code> 处理。</p> <p>首先我们需要在 sales 目录下面创建一个新的文件 <code>api\urls.py</code> 。</p> <p>然后在这个 <code>api\urls.py</code> 文件中输入如下内容</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> django<span class="token punctuation">.</span>urls <span class="token keyword">import</span> path
<span class="token keyword">from</span> <span class="token punctuation">.</span> <span class="token keyword">import</span> views

urlpatterns <span class="token operator">=</span> <span class="token punctuation">[</span>
    path<span class="token punctuation">(</span><span class="token string">'tests/'</span><span class="token punctuation">,</span> views<span class="token punctuation">.</span>tests<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>然后，我们再修改主url路由文件 <code>config/urls.py</code> , 如下</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> django<span class="token punctuation">.</span>contrib <span class="token keyword">import</span> admin
<span class="token comment"># 导入一个include函数</span>
<span class="token keyword">from</span> django<span class="token punctuation">.</span>urls <span class="token keyword">import</span> path<span class="token punctuation">,</span> include

urlpatterns <span class="token operator">=</span> <span class="token punctuation">[</span>
    path<span class="token punctuation">(</span><span class="token string">'admin/'</span><span class="token punctuation">,</span> admin<span class="token punctuation">.</span>site<span class="token punctuation">.</span>urls<span class="token punctuation">)</span><span class="token punctuation">,</span>

    <span class="token comment"># 凡是 url 以 api/  开头的，</span>
    <span class="token comment"># 都根据 api.urls 里面的 子路由表进行路由</span>
    path<span class="token punctuation">(</span><span class="token string">'api/'</span><span class="token punctuation">,</span> include<span class="token punctuation">(</span><span class="token string">'api.urls'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>当一个http请求过来时， Django检查 url，比如这里是<code>api/tests/</code>，</p> <p>先到主url路由文件 <code>config/urls.py</code>中查看 是否有匹配的路由项。</p> <p>如果有匹配 ( 这里匹配了 <code>api/</code> )， 并且匹配的对象 不是 函数， 而是 一个子路由设置 , 比如这里是 <code>include('api.urls')</code></p> <p>就会去子路由文件中查看， 这里就是 api.urls 对应的文件 <code>api\urls.py</code> 。</p> <p>注意这时，会从请求url中去掉 前面主路由文件 已经匹配上的部分（这里是 <code>api/</code> ）, 将剩余的部分 （这里是 <code>tests/</code> ）去子路由文件中查看是否有匹配的路由项。</p> <p>这里就匹配了 <code>tests/</code> ，匹配的对象，这里是 <code>views.tests</code> ，它是一个处理函数，就调用该函数处理 这个http请求， 将该函数的返回对象 构建 HTTP响应消息，返回给客户端。</p> <h3 id="django链接mysql"><a href="#django链接mysql" class="header-anchor">#</a> Django链接MYSQL</h3> <p>Django官方已经不建议使用pymysql库了，而是改用mysqlclient，直接pip安装即可。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>pip install mysqlclient
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;">Django的基础配置文件:</font> <code>config/settings.py</code></p> <p>默认情况下, Django连接的是自带的<code>sqlite</code>数据库</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>DATABASES <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">'default'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">'ENGINE'</span><span class="token punctuation">:</span> <span class="token string">'django.db.backends.sqlite3'</span><span class="token punctuation">,</span>
        <span class="token string">'NAME'</span><span class="token punctuation">:</span> BASE_DIR <span class="token operator">/</span> <span class="token string">'db.sqlite3'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><font style="color:blue;">连接mysql分两步:</font></p> <ul><li><p>第一步: Mysql提前创建好数据库</p></li> <li><p>第二步: 修改Django的<code>settings.py</code>文件</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>DATABASES <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">'default'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">'ENGINE'</span><span class="token punctuation">:</span> <span class="token string">'django.db.backends.mysql'</span><span class="token punctuation">,</span>  <span class="token comment"># 数据库类型</span>
        <span class="token string">'NAME'</span><span class="token punctuation">:</span> <span class="token string">'Django'</span><span class="token punctuation">,</span>  <span class="token comment"># 数据库名称</span>
        <span class="token string">'USER'</span><span class="token punctuation">:</span> <span class="token string">'root'</span><span class="token punctuation">,</span>  <span class="token comment"># 数据库账号</span>
        <span class="token string">'PASSWORD'</span><span class="token punctuation">:</span> <span class="token string">'chuan123456@'</span><span class="token punctuation">,</span>  <span class="token comment"># 数据库密码</span>
        <span class="token string">'HOST'</span><span class="token punctuation">:</span> <span class="token string">'mysql'</span><span class="token punctuation">,</span>  <span class="token comment"># mysql的地址</span>
        <span class="token string">'PORT'</span><span class="token punctuation">:</span> <span class="token string">'3306'</span><span class="token punctuation">,</span>  <span class="token comment"># mysql的端口号</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li></ul></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">4/6/2022, 6:44:40 PM</span></div></footer> <!----> </main></div><div class="global-ui"><!----><!----><!----></div></div>
    <script src="/blog/assets/js/app.160d1732.js" defer></script><script src="/blog/assets/js/2.553eb4dc.js" defer></script><script src="/blog/assets/js/26.81508f46.js" defer></script><script src="/blog/assets/js/3.4c6b6694.js" defer></script>
  </body>
</html>
